﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class panel_Unit : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        public panel_Unit()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);

            LoadData();

            this.Unit.DoubleClick += Unit_DoubleClick;
            this.Unit.PopupMenuShowing += Unit_PopupMenuShowing;
            this.unitContext.ItemClicked += unitContext_ItemClicked;
        }

        private void callEditForm()
        { 
            DataRow row = Unit.GetFocusedDataRow();
            frm_Edit_Unit editForm = new frm_Edit_Unit();
            editForm.unit_id = row[0].ToString();
            if (editForm.LoadData())
            {
                //editForm.Show();
                editForm.ShowDialog();
                LoadData();
            }
            else
            {
                MessageBox.Show("Cannot load data for selected record");
            } 
        }

        void Unit_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        void unitContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.unitContext.Hide();
            DataRow row = Unit.GetFocusedDataRow();
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_Unit add_Form = new frm_Add_Unit();
                    //add_Form.Show();
                    add_Form.ShowDialog();
                    LoadData(); 
                    break;
                case "deleteSelection":
                    deleteRecord();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void Unit_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.unitContext.Show(MousePosition);
        }

        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT * FROM [222_Unit]";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridUnit.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                MessageBox.Show("There is no data for Unit");
                DataTable newTable = new DataTable();
                newTable.Columns.Add("UnitID", typeof(string));
                newTable.Columns.Add("UnitNameEN", typeof(string));
                newTable.Columns.Add("UnitNameVN", typeof(string));
                gridUnit.DataSource = newTable;
            }
            Unit.Columns[0].Caption = "Unit ID";
            Unit.Columns[1].Caption = "Unit Name English";
            Unit.Columns[2].Caption = "Unit Name Vietnamese";
            Unit.BestFitColumns();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            frm_Add_Unit add_Form = new frm_Add_Unit();
            //add_Form.Show();
            add_Form.ShowDialog();
            LoadData(); 
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteRecord();
        }

        private void deleteRecord()
        {
            if (MessageBox.Show("Are you really to delete selected record now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                DataRow row = Unit.GetFocusedDataRow();
                try
                {
                    myConnection.Open();
                    string sql = "DELETE [222_Unit] WHERE UnitID='" + row[0].ToString() + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    LoadData();
                    MessageBox.Show("Unit deleted success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot delete Unit. Please try again.");
                }
            }
        }
    }
}
